<template>
  <div>
    <el-input
      v-model.trim="typeName"
      @change="$refs.typesTree.filter(typeName)"
    ></el-input>
    <el-tree
      ref="typesTree"
      :data="treeNodes.children"
      node-key="typeId"
      :props="treeStyle"
      default-expand-all
      show-checkbox
      :filter-node-method="searchByName"
    ></el-tree>
  </div>
</template>

<script>
import Convert from "@/utils/TreeFlatConvert";
import { commodityModuleApi } from "@/api/apis";
// import  New from './components/New.vue';
export default {
  name: "GoodsType",
  components: {
    // New,Old,
  },
  props: {
    sampleP: {
      type: Number,
      default: 0,
    },
  },
  /**
  * typeId: 2,
   typeName: "果汁饮料",
   typeParentId: 1,
   typeLevel: 2,
   children: [
            {
              typeId: 3,
              typeName: "椰子汁",
              typeParentId: 2,
              typeLevel: 3,
              children: [],
            },
            {
              typeId: 4,
              typeName: "梨汁",
              typeParentId: 2,
              typeLevel: 3,
              children: [],
            },
  * 
  * */
  data() {
    return {
      sampleD: "value",
      // flatData
      // treeNodes: {},
      treeStyle: {
        children: "children",
        label: "typeName",
      },
      typeName: null,
    };
  },
  computed: {
    // sampleC() {
    //     return this.sampleD+'--';
    // }
    treeNodes() {
      return this.$store.state.commodityTypesTree;
    },
  },
  created() {},
  mounted: function () {
    // this.$http({
    //         method: "post",
    //         url: "/",
    //         data: this,
    //       })
    // commodityModuleApi.commodityTypesApi().then(
    //   (res) => {
    //     let flatNodes = JSON.parse(JSON.stringify(res.data));
    //     //添加根节点 添加children属性
    //     let root = {
    //       typeId: 0,
    //       typeName: "虚拟根节点",
    //       typeParentId: -1,
    //       typeLevel: 1,
    //     };
    //     flatNodes.push(root);
    //     flatNodes.forEach((x) => (x.children = []));
    //
    //     let c = new Convert({
    //       flatNodes: flatNodes,
    //     });
    //     //添加虚拟根节点
    //
    //     this.treeNodes = c.toTreeDirect("typeId", "typeParentId");
    //     this.$message.warning(JSON.stringify(this.treeNodes));
    //   },
    //   (error) => {
    //     console.log(" request error : " + error.response.status);
    //   }
    // );
  },
  methods: {
    sampleM() {},
    searchByName(value, data, node) {
      // data.typeName like '%'value"%"
      return data.typeName.includes(value);
    },
  },
  watch: {
    // sampleC(newValue, oldValue) {
    //
    // }
  },
};
</script>

<!--<style scoped>-->
<style lang="scss" scoped></style>
